/*
 * Copyright 2013-2015 the original author or authors.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.xdtech.web.freemark.tags;

import java.io.IOException;
import java.util.Map;
import java.util.UUID;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;

/**
 * 
 * @author max.zheng
 * @create 2015-4-25下午9:26:02
 * @since 1.0
 * @see
 */
public class CombogridTag extends DataGridTag {

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-25下午9:26:20
	 * @modified by
	 * @param env
	 * @param params
	 * @param body
	 * @throws IOException
	 * @throws TemplateException
	 */
	@Override
	public void render(Environment env, Map params, TemplateDirectiveBody body)
			throws IOException, TemplateException {
//		<input id="linkageOperationsHidden" type="hidden" name="linkageOperations" />
//		<select id="linkageOperations"  class="easyui-combogrid" style="width:250px" data-options="
//				panelWidth: 500,
//				multiple: true,
//				idField: 'id',
//				textField: 'name',
//				url: 'deviceOperation.do?loadList',
//				method: 'get',
//				columns: [[
//					{field : 'id',checkbox : true},
//					{field : 'name',width : 100,title : '动作名称',},
//					{field : 'type',width : 100,title : '动作类型',formatter : function(value,row, index) {if ((value + '') == '1') {return '动作模式';} else if ((value + '') == '2') {return '场景模式';}},}
//				]],
//				fitColumns: true,
//				onChange : function(a, b) {getSelects()}
//			">
//		</select>
		//标签方式
		try {
			StringBuffer sb = new StringBuffer();
			String id = params.get("id")==null?UUID.randomUUID().toString():params.get("id").toString();
			String name = params.get("name")==null?"none":params.get("name").toString();
			String width = params.get("width")==null?"159":params.get("width").toString();
			String multiple = params.get("multiple")==null?"":params.get("multiple").toString();
			String required = params.get("required")==null?"false":params.get("required").toString();
			String className = params.get("item").toString();
			
			sb.append("<input id='"+id+"Hidden' type='hidden' name='"+name+"' />");
			sb.append("<select id='"+id+"'  class='easyui-combogrid' style='width:"+width+"px' data-options='");
			sb.append("panelWidth: 500,")
			  .append(addProperties("multiple", params.get("multiple"),false, false))
			  .append(addProperties("idField", params.get("idField"),"id", true))
			  .append(addProperties("textField", params.get("textField"),"name", true))
			  .append(addProperties("url", params.get("url"),"", true))
			  .append("fitColumns: true,");
			sb.append(createColumns(className,params.get("idField")!=null,null,false));
			sb.append("onChange : function(a, b) {")
			  .append("$('#"+id+"hidden').val( $('#"+id+"').combogrid('getValues'));")
			  .append("}");
			sb.append("'></select>");
			
			
			writeBody(env, sb.toString());
		} catch (Exception e) {
			// TODO: handle exception
		}
		
	}

}
